﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net.MVC;
using Buffalo.Tourism.DAL;
using Buffalo.Tourism.Enum;
using Buffalo.Tourism.DAL.Models;
using Buffalo.Lib.Common;
using Buffalo.Administration.DAL;
using Buffalo.Administration.Enum;

namespace Buffalo.Web.Areas.Tourism.Controllers
{
    public class CatalogController : BaseController
    {
        #region Restaurant
        public AjaxStoreResult GetListRestaurant(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Restaurant> list = new CatalogDAL().GetListRestaurant(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Restaurant { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {                
                throw;
            }
        }
        public AjaxStoreResult SaveRestaurants()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveRestaurants(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult RestaurantList()
        {
            return CheckPermission( "Restaurant");
        }

        #endregion
        #region Company
        public AjaxStoreResult GetListCompany(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Company> list = new CatalogDAL().GetListCompany(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Company { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveCompanies()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveCompanies(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult CompanyList()
        {
            return CheckPermission( "Company");
        }

        #endregion

        #region Broker
        public AjaxStoreResult GetListBroker(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Broker> list = new CatalogDAL().GetListBroker(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Broker { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveBrokers()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveBrokers(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult BrokerList()
        {
            return CheckPermission( "Broker");
        }

        #endregion
        #region Hotel
        public AjaxStoreResult GetListHotel(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Hotel> list = new CatalogDAL().GetListHotel(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Hotel { ID = GeneralConstant.BLANK_ITEM, Name = "..." });
                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveHotels()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveHotels(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult HotelList()
        {
            return CheckPermission( "Hotel");
        }

        #endregion
        #region Language
        public AjaxStoreResult GetListLanguage(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Language> list = new CatalogDAL().GetListLanguage(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Language { ID = GeneralConstant.BLANK_ITEM, Name = "..." });
                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveLanguages()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveLanguages(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult LanguageList()
        {
            return CheckPermission( "Language");
        }

        #endregion
        #region Nationality
        public AjaxStoreResult GetListNationality(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Nationality> list = new CatalogDAL().GetListNationality(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Nationality { ID = GeneralConstant.BLANK_ITEM, Name = "..." });
                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveNationalities()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveNationalities(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult NationalityList()
        {
            return CheckPermission( "Nationality");
        }

        #endregion
        #region Partner
        public AjaxStoreResult GetListPartner(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Partner> list = new CatalogDAL().GetListPartner(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Partner { ID = GeneralConstant.BLANK_ITEM, Name = "..." });
                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SavePartners()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SavePartners(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult PartnerList()
        {
            return CheckPermission( "Partner");
        }

        #endregion
        #region Employee
        public AjaxStoreResult GetListEmployee(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Employee> list = new CatalogDAL().GetListEmployee(limit, start, dir, sort, criteria, searchText, out total);
                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveEmployees()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveEmployees(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult EmployeeList()
        {
            return CheckPermission( "Employee");
        }

        #endregion

        #region Service
        public AjaxStoreResult GetListService(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Service> list = new CatalogDAL().GetListService(limit, start, dir, sort, criteria, searchText, out total);
                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveServices()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveServices(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult ServiceList()
        {
            bool hasViewPermision = new PermissionDAL().HasPermission(GeneralVar.gLoggedUserName, "Service", enuPermissionType.View);
            if (hasViewPermision)
                return this.View();
            return null;
        }

        #endregion
        #region Vehicle
        public AjaxStoreResult GetListVehicle(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Vehicle> list = new CatalogDAL().GetListVehicle(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Vehicle { ID = GeneralConstant.BLANK_ITEM, Name = "..." });
                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveVehicles()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveVehicles(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult VehicleList()
        {
            bool hasViewPermision = new PermissionDAL().HasPermission(GeneralVar.gLoggedUserName, "Vehicle", enuPermissionType.View);
            if (hasViewPermision)
                return this.View();
            return null;
        }

        #endregion
    }
}
